package com.chengjue.service.impl;

import com.chengjue.common.ServerResponse;
import com.chengjue.mapper.TbItemCatMapper;
import com.chengjue.pojo.TbItemCat;
import com.chengjue.pojo.TbItemCatExample;
import com.chengjue.pojo.TbItemExample;
import com.chengjue.pojo.TreeNode;
import com.chengjue.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    TbItemCatMapper itemCatMapper;
    @Override
    public ServerResponse getItemCatByParentId(Long id) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria= example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbItemCat> list = itemCatMapper.selectByExample(example);
        if(null!= list & list.size()>0){
            List<TreeNode> treeNodes = new ArrayList<>();
            for (TbItemCat item:list ) {
                TreeNode treeNode = new TreeNode();
                treeNode.setId(item.getId());
                treeNode.setText(item.getName());
                treeNode.setState(item.getIsParent() ? "closed":"open");
                treeNodes.add(treeNode);
            }

            return ServerResponse.createBySuccess(treeNodes);
        }
        return ServerResponse.createByErrorMessage("参数错误");
    }
}
